local function foo()
   print("in the function")
   --dosomething()
   local x = 10
   local y = 20
   return x + y
end

local a = foo    --把函数赋给变量

print("functions do not have names")

print(a())

local my_math = {
   add,
   sub,
   mul,
   div
}

print(my_math.add) --> nil

function my_math.add(a, b)
   return a + b
end

function my_math.sub(a, b)
   return a - b
end

function my_math.mul(a, b)
   return a * b
end

function my_math.div(a, b)
   return a / b
end

print(my_math.add(2, 3))
print(my_math.sub(2, 3))
print(my_math.mul(2, 3))
print(my_math.div(5, 2))

--a = {p = print}
--a.p("hello world") --> hello world
--print = math.sin   --> 'print' now refers to the sine function
--a.p(print(1))      --> 0.841470
--sin = a.p          --> 'sin' now refers to the print function
--sin(10, 20)        --> 10 20

-- 这个函数等价于下面的写法
-- 这个写法是下面写法的语法糖形式
function foo (x) return 2 * x end

foo = function (x) return 2*x end

network = {
   {name = "grauna",   IP = "210.26.30.34"},
   {name = "arraial",  IP = "210.26.30.23"},
   {name = "lua",      IP = "210.26.23.12"},
   {name = "derain",   IP = "210.26.23.20"},
}

table.sort(network, function (a, b) return (a.name > b.name) end)

print("closure")
function new_counter()
   local i = 0
   return function () -- anonymous function
	  i = i + 1
	  return i
   end
end

c1 = new_counter()
print(c1())  -->1
print(c1())  -->2

c2 = new_counter()
print(c2())  -->1
print(c1())  -->3
print(c2())  -->2

